package willevaluate.activity;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.MyLogUtils;
import view.MyConfirmDialog;
import willevaluate.adapter.QuestionnaireMajorAdapter;
import willevaluate.bean.QuestionnaireMajorBean;
import willevaluate.model.QuestionnaireMajorModel;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 * 问卷调查的专业多选列表
 */

@ContentView(R.layout.questionnaire_major)
public class QuestionnaireMajor extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.common_detail)
    private TextView common_detail;
    @ViewInject(R.id.questionnaire_major_lv)
    private ExpandableListView questionnaire_major_lv;
    @ViewInject(R.id.questionnaire_major_confirm)
    private Button questionnaire_major_confirm;
    private MyHandler1 handler;
    private ArrayList<QuestionnaireMajorModel> arrayList;
    private ArrayList<QuestionnaireMajorModel> tempArrayList;//对比用的数组
    private QuestionnaireMajorAdapter questionnaireMajorAdapter;
    private View header;
    private static final String TAG = "QuestionnaireMajor";

    private static class MyHandler1 extends Handler {
        WeakReference<QuestionnaireMajor> weakReference;

        public MyHandler1(QuestionnaireMajor questionnaireMajor) {
            weakReference = new WeakReference<>(questionnaireMajor);
        }

        @Override
        public void handleMessage(Message msg) {
            QuestionnaireMajor activity = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    QuestionnaireMajorBean questionnaireMajorBean = gson.fromJson((String) msg.obj, QuestionnaireMajorBean.class);
                    if (questionnaireMajorBean != null) {
                        List<QuestionnaireMajorBean.DataBean> data = questionnaireMajorBean.getData();
                        if (data != null) {
                            for (QuestionnaireMajorBean.DataBean dataBean :
                                    data) {
                                QuestionnaireMajorModel questionnaireMajorModel = new QuestionnaireMajorModel();
                                questionnaireMajorModel.setName(dataBean.getName());
                                questionnaireMajorModel.setCount(dataBean.getCount());
                                List<String> list = dataBean.getList();
                                List<QuestionnaireMajorModel.DataDetail> beanList = new ArrayList<>();
                                if (list != null) {
                                    for (int i = 0; i < list.size(); i++) {
                                        QuestionnaireMajorModel.DataDetail dataDetail = new QuestionnaireMajorModel.DataDetail();
                                        dataDetail.setName(list.get(i));
                                        beanList.add(dataDetail);
                                    }
                                    questionnaireMajorModel.setBeanList(beanList);
                                }
                                activity.arrayList.add(questionnaireMajorModel);
                            }
                            if (activity.tempArrayList.size() == activity.arrayList.size()) {
                                for (int i = 0; i < activity.arrayList.size(); i++) {
                                    if (activity.tempArrayList.get(i).getName().equals(activity.arrayList.get(i).getName())) {
                                        activity.arrayList.get(i).setCount(activity.tempArrayList.get(i).getCount());
                                        List<QuestionnaireMajorModel.DataDetail> beanList = activity.arrayList.get(i).getBeanList();
                                        List<QuestionnaireMajorModel.DataDetail> beanList1 = activity.tempArrayList.get(i).getBeanList();
                                        if (beanList != null && beanList1 != null) {
                                            for (int j = 0; j < beanList.size(); j++) {
                                                beanList.get(j).setChecked(beanList1.get(j).isChecked());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    activity.questionnaireMajorAdapter.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new MyHandler1(this);
        tempArrayList = (ArrayList<QuestionnaireMajorModel>) getIntent().getSerializableExtra("majorsList");
        initView();
        getData();
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("专业选择");
        common_detail.setText("清空");
        common_detail.setVisibility(View.VISIBLE);
        common_detail.setOnClickListener(this);
        header = LayoutInflater.from(this).inflate(R.layout.questionnaire_prov_group, null);
        ((TextView) header.findViewById(R.id.questionnaire_prov_group_tv)).setText("选择你想学的专业");
        questionnaire_major_confirm.setOnClickListener(this);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
        }
        questionnaireMajorAdapter = new QuestionnaireMajorAdapter(arrayList, this);
        questionnaire_major_lv.addHeaderView(header);
        questionnaire_major_lv.setAdapter(questionnaireMajorAdapter);
        questionnaire_major_lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                arrayList.get(groupPosition).getBeanList().get(childPosition).setChecked(!arrayList.get(groupPosition).getBeanList().get(childPosition).isChecked());
                if (arrayList.get(groupPosition).getBeanList().get(childPosition).isChecked()) {
                    arrayList.get(groupPosition).setCount(arrayList.get(groupPosition).getCount() + 1);
                } else {
                    arrayList.get(groupPosition).setCount(arrayList.get(groupPosition).getCount() - 1);
                }
                questionnaireMajorAdapter.notifyDataSetChanged();
                return false;
            }
        });
    }

    MyConfirmDialog myConfirmDialog;

    @Override
    public void onBackPressed() {
        myConfirmDialog = new MyConfirmDialog(this, "是否保存", "是", "否");
        myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                MyConfirm();
            }
        });
        myConfirmDialog.setCancelListener(new MyConfirmDialog.CancelListener() {
            @Override
            public void cancel() {
                QuestionnaireMajor.this.finish();
            }
        });
        myConfirmDialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                myConfirmDialog = new MyConfirmDialog(this, "是否保存", "是", "否");
                myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
                    @Override
                    public void confirm() {
                        MyConfirm();
                    }
                });
                myConfirmDialog.setCancelListener(new MyConfirmDialog.CancelListener() {
                    @Override
                    public void cancel() {
                        QuestionnaireMajor.this.finish();
                    }
                });
                myConfirmDialog.show();
                break;
            case R.id.common_detail:
                for (QuestionnaireMajorModel questionnaireMajorModel :
                        arrayList) {
                    questionnaireMajorModel.setCount(0);
                    List<QuestionnaireMajorModel.DataDetail> beanList = questionnaireMajorModel.getBeanList();
                    for (int i = 0; i < beanList.size(); i++) {
                        beanList.get(i).setChecked(false);
                    }
                }
                questionnaireMajorAdapter.notifyDataSetChanged();
                break;
            case R.id.questionnaire_major_confirm:
                MyConfirm();
                break;
            default:
                break;
        }
    }

    private void MyConfirm() {
        String majors = "";
        StringBuffer stringBuffer = new StringBuffer(majors);
        for (QuestionnaireMajorModel questionnaireMajorModel :
                arrayList) {
            List<QuestionnaireMajorModel.DataDetail> beanList = questionnaireMajorModel.getBeanList();
            for (int i = 0; i < beanList.size(); i++) {
                if (beanList.get(i).isChecked()) {
                    stringBuffer.append("/" + beanList.get(i).getName());
                }
            }
        }
        majors = stringBuffer.toString();
        String substring;
        if (majors.length() > 1) {
            substring = majors.substring(1, majors.length());
        } else {
            substring = majors;
        }
        Intent i = new Intent();
        i.putExtra("majors", substring);
        i.putExtra("majorsList", arrayList);
        setResult(1, i);
        this.finish();
        myConfirmDialog = null;
    }


    private void getData() {
        new InterNetController(this, Constant.MUTABLESELECTMAJOR, handler, null, 0);
    }
}
